package test.san.baseproject.presenter.impl

import android.util.Log
import test.san.baseproject.base.BaseView
import test.san.baseproject.model.BaseBean
import test.san.baseproject.net.HomeRequest
import test.san.baseproject.net.NetManager
import test.san.baseproject.net.ResponseHandler
import test.san.baseproject.presenter.interf.HomePresenter
import test.san.baseproject.util.URLProviderUtils
import test.san.baseproject.view.HomeView


class HomePresenterImp(var homeView: BaseView<List<BaseBean>>?): HomePresenter {


    override fun destoryView() {
        if (homeView != null) {
            homeView = null
        }
    }

    override fun loadDatas() {
        // 封装网络请求之后的处理
        HomeRequest(URLProviderUtils.getHomeUrl(0,0) ,object : ResponseHandler<List<BaseBean>> {
            override fun onError(msg: String?) {
                Log.e("HomePresenterImp", "请求数据失败")
                homeView?.onError(msg)
            }

            override fun onSuccess(result: List<BaseBean>?) {
                Log.e("onSuccess", "result = ${result}")
                homeView?.loadSuccess(result)
            }
        }).excute()

//        NetManager.manager.sendRequest(request)



//        // 测试地址
//        val path = "http://192.168.31.195/index.php/api/mytest/getinfo"
//        val client = OkHttpClient()
//        val request = Request.Builder().url(path).get().build()
//        client.newCall(request).enqueue(object : Callback {
//            override fun onFailure(call: Call, e: IOException) {
//                Log.e("HomeFragment", "获取数据失败")
//                ThreadUtil.runOnMainThread(object :Runnable{
//                    override fun run() {
//                        // 回调到view层处理
//                        homeView.onError(e.message)
//                    }
//                })
//            }
//
//            override fun onResponse(call: Call, response: Response) {
//                val dataString = response.body?.string()
//                Log.e("HomeFragment", "获取数据成功 data = ${dataString}")
//
//                val gson = Gson()
//                // 转模型数组
//                val bean = gson.fromJson<List<BaseBean>>(dataString, object : TypeToken<List<BaseBean>>(){}.type)
//                // 转单个模型
////                val bean = gson.fromJson(dataString, BaseBean::class.java)
//                Log.e("HomeFragment", "bean = ${bean}")
//
//                // 刷新列表
//                ThreadUtil.runOnMainThread(object :Runnable{
//                    override fun run() {
////                        refresh.isRefreshing = false
////                        homeAdapter.moreList(bean as ArrayList<BaseBean>)
//                        homeView.loadSuccess(bean)
//                    }
//                })
//            }
//        })
    }

    override fun loadMore() {
        HomeRequest(URLProviderUtils.getHomeUrl(0,0), object : ResponseHandler<List<BaseBean>> {
            override fun onError(msg: String?) {
                Log.e("HomePresenterImp", "请求数据失败")
                homeView?.onError(msg)
            }

            override fun onSuccess(result: List<BaseBean>?) {
                Log.e("onSuccess", "result = ${result}")
                homeView?.loadSuccess(result)
            }

//            override fun onSuccess(result: List<BaseBean>?) {
//                homeView?.loadMore(result)
//            }
        }).excute()
//        NetManager.manager.sendRequest(request)

// 测试地址
//        val path = "http://192.168.31.195/index.php/api/mytest/getinfo"
//        val client = OkHttpClient()
//        val request = Request.Builder().url(path).get().build()
//        client.newCall(request).enqueue(object : Callback{
//            override fun onFailure(call: Call, e: IOException) {
//                Log.e("HomeFragment", "获取数据失败")
//                ThreadUtil.runOnMainThread(object :Runnable{
//                    override fun run() {
////                        refresh.isRefreshing = false
//
//                        homeView.onError(e.message)
//                    }
//                })
//            }
//
//            override fun onResponse(call: Call, response: Response) {
//                val dataString = response.body?.string()
//                Log.e("HomeFragment", "获取数据成功 data = ${dataString}")
//
//                val gson = Gson()
//                val bean = gson.fromJson<List<BaseBean>>(dataString, object : TypeToken<List<BaseBean>>(){}.type)
////                val bean = gson.fromJson(dataString, BaseBean::class.java)
//                Log.e("HomeFragment", "bean = ${bean}")
//
//                // 刷新列表
//                ThreadUtil.runOnMainThread(object :Runnable{
//                    override fun run() {
////                        refresh.isRefreshing = false
////                        homeAdapter.updateList(bean as ArrayList<BaseBean>)
//
//                        homeView.loadMore(bean)
//                    }
//                })
//            }
//        })
    }
}